# coding = utf-8
from rest_framework import status
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from wxchat.api.permissions import WeixinPermission
from .serializers import InvestigationSerializer, QuestionSerializer, GradeSerializer, InvestResultSerializer
from question.models import Grade, Investigation, Question, InvestResult


class InvestigationListAPIView(ListAPIView):
    authentication_classes = []
    permission_classes = (WeixinPermission,)
    pagination_class = None
    queryset = Investigation.objects.all()
    serializer_class = InvestigationSerializer


class QuestionListAPIView(ListAPIView):
    authentication_classes = []
    permission_classes = (WeixinPermission,)
    pagination_class = None
    queryset = Question.objects.all()
    serializer_class = QuestionSerializer

    def get_queryset(self):
        queryset = super(QuestionListAPIView, self).get_queryset()
        invest_id = self.request.query_params.get("invest_id", None)
        if invest_id:
            queryset = queryset.filter(invest_id=invest_id)

        return queryset


class GradeListAPIView(ListAPIView):
    authentication_classes = []
    permission_classes = (WeixinPermission,)
    pagination_class = None
    queryset = Grade.objects.all()
    serializer_class = GradeSerializer


class InvestResultViewSet(ModelViewSet):
    authentication_classes = ()
    permission_classes = (WeixinPermission,)
    pagination_class = None
    queryset = InvestResult.objects.all()
    serializer_class = InvestResultSerializer

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        invest_id = serializer.validated_data["invest"]
        openid = serializer.validated_data["openid"]
        result = InvestResult.objects.filter(openid=openid, invest_id=invest_id).first()
        if result:
            partial = kwargs.pop('partial', False)
            serializer = self.get_serializer(result, data=request.data, partial=partial)
            serializer.is_valid(raise_exception=True)
            self.perform_update(serializer)
            if getattr(result, '_prefetched_objects_cache', None):
                # If 'prefetch_related' has been applied to a queryset, we need to
                # forcibly invalidate the prefetch cache on the instance.
                result._prefetched_objects_cache = {}

            return Response(serializer.data)
        else:
            self.perform_create(serializer)
            headers = self.get_success_headers(serializer.data)
            return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

    def get_queryset(self):
        queryset = super().get_queryset()
        openid = self.request.query_params.get("openid", None)
        if openid:
            queryset = queryset.filter(openid=openid)

        return queryset







